package com.sc.sc250601.cook.config;

import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

// 跨域过滤器
//@Configuration
public class CorsFilterConfig {
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 参数1：允许跨域的路径；参数2：跨域配置信息
        source.registerCorsConfiguration("/**",config());
        return new CorsFilter(source);
    }
    @Bean
    public CorsConfiguration config() {
        CorsConfiguration config = new CorsConfiguration();
        // 允许跨域的域名
        config.addAllowedOriginPattern("*");
        // 允许跨域传递的头部信息
        config.addAllowedHeader("*");
        // 允许跨域的请求方式
        config.addAllowedMethod("*");
        //config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
        // 是否允许跨域传递 cookie
        config.setAllowCredentials(true);
        // 设置每次跨域允许的最大时间，单位秒
        config.setMaxAge(3600L);
        return config;
    }
}
